package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.dao.SetmealMapper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Transactional
@Service(interfaceClass = SetmealService.class)
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    SetmealMapper setmealMapper;

    @Autowired
    JedisPool jedisPool;
//114

    @Override
    public Result add(Integer[] ids, Setmeal setmeal) {
        setmealMapper.insert(setmeal);
        //当数组不为空
        if(ids.length!=0){
            //添加中间表信息
            Map map =new HashMap();
            map.put("id",setmeal.getId());
            map.put("ids",ids);
            setmealMapper.addSetmealCheckgroup(map);
        }
        //将图片信息存入redis中，判断图片是否为null
        if(setmeal.getImg()!=null) {
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES, setmeal.getImg());
        }
        return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
    }
    @Override
    public Result update(Integer[] ids,Setmeal setmeal) {
        //先通过id查询之前的图片名称
        Result byId = findById(setmeal.getId());
        Setmeal setmeal1 = (Setmeal) byId.getData();
        String name = setmeal.getName();
            //修改套餐表
            setmealMapper.updateById(setmeal);
            //删除中间表信息
            setmealMapper.deleteSp(setmeal.getId());
            //添加中间表信息
            Map map = new HashMap();
            map.put("id", setmeal.getId());
            map.put("ids", ids);
            setmealMapper.addSetmealCheckgroup(map);
            //没有生成垃圾图片
            if(setmeal.getImg()==null&&name==null){
                //不操作缓存，没有垃圾图片产生
                return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
            }
            //之前数据库中存在图片，没有上传图片
            if(name!=null&&setmeal.getImg()==null){
                //将redis中之前的键删除
                jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_DB_RESOURCES,name);
                return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
            }
            //之前数据库中没有上传图片，修改后上传了图片
            if(name==null&&setmeal.getImg()!=null){
                //向redis中添加图片信息
                jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
                return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
            }else {
                return null;
            }
    }

    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        Page<Setmeal> page = new Page(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        List list;
        if (queryPageBean.getQueryString() != null && queryPageBean.getQueryString().length() > 0) {
            list = setmealMapper.selectPage(page,
                    new EntityWrapper<Setmeal>()
                            .eq("code", queryPageBean.getQueryString())
                            .or()
                            .eq("name", queryPageBean.getQueryString())
                            .or()
                            .eq("helpCode",queryPageBean.getQueryString())
            );
        } else {
            list = setmealMapper.selectPage(page, null);
        }
        return new PageResult(page.getTotal(), list);
    }

    @Override
    public Result findById(int id) {
        Setmeal setmeal = setmealMapper.selectById(id);
        return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
    }

    @Override
    public Result delete(int id) {
        return null;
    }

    @Override
    public Result findBySp(Integer id) {
        List bySp = setmealMapper.findBySp(id);
        return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,bySp);
    }

}
